package com.zhuangjie.openfeign.aop.retry.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 重试的线程池
 *
 * @author zhuangjie
 * @date 2023/11/08
 */
@Configuration
public class RetryableThreadPoolConfig {
    @Bean
    public RetryableThreadPool retryableThreadPoolConfig() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 20, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(200), new ThreadFactory() {
            private final AtomicInteger threadNumber = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("spark系统线程" + threadNumber.getAndIncrement());
                return thread;
            }
        }, new ThreadPoolExecutor.CallerRunsPolicy());
        return new RetryableThreadPool(threadPoolExecutor);
    }
}
